//
//  ViewController.m
//  23-QQ音乐
//
//  Created by 曹曹 on 16/8/15.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "MVModel.h"
#import "MVData.h"
#import "MVContents.h"
#import "MVTableList.h"
#import "MVConllectionList.h"
#import "MVShowPlay.h"
#import "MVSingers.h"
#import "MVTableViewCell.h"

#define kTableViewCellReuse @"tableViewCellReuse"
#define kQQ_MV_URLString @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1"

@interface ViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *qq_MVTableView;
@property (nonatomic, strong) MVModel *mvModel;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
}

- (void)requestData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:kQQ_MV_URLString parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        // 转成字符串
        NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        // 处理字符串
        dataString = [dataString stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""];
        dataString = [dataString substringToIndex:dataString.length - 1];
        // 转成NSData
        NSData *finalData = [dataString dataUsingEncoding:NSUTF8StringEncoding];
        // JSON解析
        NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:finalData options:NSJSONReadingMutableContainers error:&error];
        self.mvModel = [MVModel modelWithDictionary:dictionary];
        [_qq_MVTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _mvModel.data.contents.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewCellReuse];
    cell.listArray = _mvModel.data.contents.list[indexPath.section].mvlist;
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return _mvModel.data.contents.list[section].name;
    // __kindof 不报警告
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
